package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogoa.R;

import artid.com.arttogoa.barcode.Bimp;
import artid.com.arttogoa.fragment.MySell_Fragment;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.volley.VolleyBasic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：MySell_Activity   
 * 类描述   ：我出售的   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-15 上午11:06:57   
 *
 */
public class MySell_Activity extends BaseActivity implements OnClickListener{
	private Context mContext;
	private RadioGroup mRadioGroup;
	private RadioButton onSaleRB,underShelfRB;
	private ViewPager mViewPager;
	private TextView mTitleLeftBtn;
	private List<Fragment> fragementlist;
	private int whichActivity=0;//默认我的出售 （0：我的出售 1：已下架）
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mysell);
		mContext=MySell_Activity.this;
		setContent(this, getIntent());
		initView();
	}

	@SuppressWarnings("deprecation")
	private void initView(){
		VolleyBasic.showLoding(mContext);
		setBack();
		mTittle.setText("我出售的");
		mTitleLeftBtn = (TextView) findViewById(R.id.mysell_title_rightBtn);
		mTitleLeftBtn.setOnClickListener(this);
		whichActivity=getIntent().getIntExtra("mySell_whichActivity", 0);
		//注册当前广播
		IntentFilter filter = new IntentFilter("com.MySell_Activity.receiver");  
		mContext.registerReceiver(broadcastReceiver, filter); 
		mRadioGroup = (RadioGroup) findViewById(R.id.mysell_RadioGroup);
		// 得到RadioGroup中的RadioButton按钮
		onSaleRB=(RadioButton) findViewById(R.id.mysell_onSaleRB);
		onSaleRB.setOnClickListener(this);
		underShelfRB=(RadioButton) findViewById(R.id.mysell_underShelfRB);
		underShelfRB.setOnClickListener(this);
		mViewPager = (ViewPager) findViewById(R.id.mysell_pager);
		fragementlist = new ArrayList<Fragment>();
		MySell_Fragment onSaleFragment = new MySell_Fragment(0);
		MySell_Fragment underShelfFragment = new MySell_Fragment(1);
		fragementlist.add(onSaleFragment);
		fragementlist.add(underShelfFragment);
		mViewPager.setAdapter(new MyPageadapter(getSupportFragmentManager()));
		// 实现滑动 绑定监听（滑动页面并改变RadioButton的改变）
		mViewPager.setOnPageChangeListener(onPage);
		mViewPager.setCurrentItem(whichActivity);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.mysell_title_rightBtn:
			//我要出售
			showPopwindow(mContext);
			break;
		case R.id.mysell_onSaleRB:
			//出售中
			mViewPager.setCurrentItem(0);
			break;
		case R.id.mysell_underShelfRB:
			//已下架
			mViewPager.setCurrentItem(1);
			break;
		default:
			break;
		}
	}

	// 实现滑动
	class MyPageadapter extends FragmentPagerAdapter {

		public MyPageadapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragementlist.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragementlist.size();
		}
	}

	OnPageChangeListener onPage=new OnPageChangeListener(){

		// 滑动页面并改变RadioButton的改变
		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			if(position==0){
				onSaleRB.setChecked(true);
			}else if(position==1){
				underShelfRB.setChecked(true);
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

	};
	
	/**
	 * 备案出售按钮显示Popwindow
	 * @param mContext
	 * @param v
	 * @param id
	 */
	public void showPopwindow(final Context mContext) {
		// 利用layoutInflater获得View
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.popupwindow_mysell, null);
		// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
		final PopupWindow window = new PopupWindow(view,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		// 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
		window.setFocusable(true);
		// 必须要给调用这个方法，否则点击popWindow以外部分，popWindow不会消失
		window.setBackgroundDrawable(new BitmapDrawable());
		// 在参照的View控件下方显示
		
		window.showAsDropDown(MySell_Activity.this.findViewById(R.id.mysell_title_rightBtn), -NetUtils.dip2px(mContext, 50),NetUtils.dip2px(mContext, 10));
		// 这里检验popWindow里的button是否可以点击
		TextView FilingQuickSellBtn = (TextView) view.findViewById(R.id.popupwindow_mysell_FilingQuickSell);
		FilingQuickSellBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 备案出售
				Intent i=new Intent(mContext,FilingQuickSell_Activity.class);
				startActivity(i);
				window.dismiss();
			}
		});
		TextView CameraSellBtn = (TextView) view.findViewById(R.id.popupwindow_mysell_CameraSell);
		CameraSellBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//拍照出售
				Intent i=new Intent(mContext,EditTakePhotosSell_Activity.class);
				i.putExtra("goodsType", Bimp.GOODS_PUBLISH);
				startActivity(i);
				window.dismiss();
			}
		});
		// popWindow消失监听方法
		window.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				System.out.println("popWindow消失");
			}
		});
	}

	/**
	 * 广播接收器
	 */
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  

		@Override  
		public void onReceive(Context context, Intent intent) {  
			String intentActivity=intent.getStringExtra("intentActivity");
			if("我出售的详情-上下架".equals(intentActivity)){
				int retrunActivity=intent.getIntExtra("retrunActivity",0);
				mViewPager.setCurrentItem(retrunActivity);
			}else if("我出售的详情-编辑".equals(intentActivity)){
				int retrunActivity=intent.getIntExtra("retrunActivity",0);
				mViewPager.setCurrentItem(retrunActivity);
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		//销毁广播
		mContext.unregisterReceiver(broadcastReceiver);
	};
}
